﻿/* 08. Write a method that adds two positive integer numbers represented as arrays of digits 
 * (each array element arr[i] contains a digit; the last digit is kept in arr[0]). 
 * Each of the numbers that will be added could have up to 10 000 digits. */

using System;
using System.Text;

public class AddsOfDigitArrays
{
    public static int[] a;
    public static int[] b;

    public static void Main()
    {
        DataInput();
        PrintAdds(Adds(a, b));
    }

    public static void DataInput()
    {
        string numberString = null;
        StringBuilder description = new StringBuilder();

        for (int i = 1; i <= 2; i++)
        {
            description.Append(string.Format("\n{0} = ", i == 1 ? "a" : "b"));

            do
            {
                Console.Clear();
                Console.Write(description.ToString());
                numberString = Console.ReadLine();
            } while (!IsNumber(numberString));

            numberString = ReverseDigits.RemoveUnsignificantZeroes(numberString);
            description.Append(numberString);
            FillArrays(i, numberString);
        }

        Console.Clear();
        Console.Write(description.ToString());
    }

    public static bool IsNumber(string numberString)
    {
        if ((numberString.Length == 0) || (numberString == null))
            return false;

        for (int i = 0; i < numberString.Length; i++)
            if ((numberString[i] < 48) || (57 < numberString[i]))
                return false;

        return true;
    }

    public static void FillArrays(int i, string numberString)
    {
        int digit;

        if (i == 1)
            a = new int[numberString.Length];
        else
            b = new int[numberString.Length];

        for (int j = numberString.Length - 1; j >= 0; j--)
        {
            digit = (int)numberString[j] - 48;

            if (i == 1)
                a[numberString.Length - 1 - j] = digit;
            else
                b[numberString.Length - 1 - j] = digit;
        }
    }

    public static int[] Adds(int[] a, int[] b)
    {
        int maxLength = Common.GetMax(a.Length, b.Length);
        int digitSum;
        int carryOne = 0;

        int[] aPlusB = new int[maxLength + 1];

        for (int i = 0; i <= maxLength; i++)
        {
            digitSum = carryOne + (i <= a.Length - 1 ? a[i] : 0) + (i <= b.Length - 1 ? b[i] : 0);
            aPlusB[i] = digitSum % 10;
            carryOne = 9 < digitSum ? 1 : 0;
        }

        return aPlusB;
    }

    public static void PrintAdds(int[] aPlusB)
    {
        for (int i = aPlusB.Length - 1; i >= 0; i--)
            if (aPlusB[i] > 0)
            {
                Console.Write("\n\na + b = ");
                for (int j = i; j >= 0; j--)
                    Console.Write(aPlusB[j]);

                break;
            }

        Console.WriteLine("\n");
    }
}